﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<script src="~/js/jquery-1.8.2.js"></script>
<script>
    $(function () {
        $.ajax({
            url: "http://localhost:19451/api/Menu/Getshow",
            type: "Get",
            data: {},
            success: function (data) {
                var tr = "";
                $(data).each(function (i, item) {

                    tr += "<tr><th><input type='checkbox' value='" + item.MenuId + "'/><th>" + item.MenuName + "</th><th><img src='" + item.MenuPhoto + "'/></th><th>" + item.MenuDescrib + "</th><th>" + item.MenuPrice + "</th><th><a href='#' onclick='add(" + item.MenuId + ")'>上架</a><a href='#' onclick='del(" + item.MenuId + ")'>删除</a></th></tr> ";
                })
                $("#t1").append(tr)
            }
        })
    })
    function add(id) {
        $.ajax({
            url: "http://localhost:19451/api/Stand/GetAddStand",
            type: "Get",
            data: { id: id },
            success: function (data) {
                if (data > 0) {
                    alert("上架成功！")
                }
                else {
                    alert("上架失败")
                }
            }
        })
    }
    function del(id)
    {
        $.ajax({
            url: "http://localhost:19451/api/Stand/GetDelMenu",
            type: "Get",
            data: { id: id },
            success: function (data) {
                if (data > 0) {
                    alert("删除成功！")
                }
                else {
                    alert("删除失败")
                }
            }
        })
    }
    function check()
    {
        var n = $("#checkbox1").prop("checked")
        $("input:checkbox").prop("checked",n)
    }

    function AddAll()
    {
        var ids = "";

        for (var i = 0; i < $("input:checkbox").length; i++) {
            if ($("input:checkbox").eq(i).attr("checked") == "checked") {
                ids += $("input:checkbox").eq(i).val() + ",";
            }
        }
        ids = ids.split(",")
        if (ids == "") {
            alert("请至少选择一项")
        }
        else {
            for (var i = 0; i < ids.length; i++) {
                $.ajax({
                    url: "http://localhost:19451/api/Stand/GetAddStand",
                    type: "Get",
                    data: { id: ids[i] },
                    success: function (data) {
                        if (data > 0) {
                        }
                    }
                })
          
            }
            alert("已全部添加成功！");
        }
    }
    function DelAll()
    {
        var ids = "";

        for (var i = 0; i < $("input:checkbox").length; i++) {
            if ($("input:checkbox").eq(i).attr("checked") == "checked") {
                ids += $("input:checkbox").eq(i).val() + ",";
            }
        }
        ids = ids.substring(0, ids.length - 1);
        alert(ids)
        $.ajax({
            url: "http://localhost:19451/api/Stand/GetDelMenu",
            type: "Get",
            data: { id: ids },
            success: function (data) {
                if (data > 0) {
                    alert("全部删除成功！")
                    location.href = "/Home/Manager";
                }
                else {
                    alert("全部删除失败")
                }
            }
        })
    }
</script>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Manager</title>
</head>
<body>
    <div> 
        <table id="t1">
            <tr>
                <th><input type="checkbox" id="checkbox1" onclick="check()" /></th><th>菜名</th><th>图片</th><th>菜单介绍</th><th>单价</th><th>操作</th>
            </tr>          
        </table>
        <input type="button" value="批量添加" onclick="AddAll()"/><input type="button" value="批量删除" onclick="DelAll()" />
    </div>
</body>
</html>
